#include "BlockQueue.hpp"
#include <unistd.h>
#include "Task.hpp"

void* consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        sleep(1);
        Task t = bq->Pop();
        t.Execute();
        std::cout << "接受了一个任务 " << t.X() << "+" << t.Y() << "=" << t.Result() << std::endl;
    }
}

void* producer(void* args)
{
    int x = 1, y = 1;
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        Task t(x, y);
        bq->Equeue(t);
        std::cout << "发布了一个任务： " << x++ << "+" << y++ << "=?" << std::endl;
    }
}

int main()
{
    BlockQueue<Task> *bq = new BlockQueue<Task>();

    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, producer, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}